package com.system

import com.commmon.VerifyCodeUtils

import javax.servlet.http.Cookie

/**
 * 验证码
 */
class VerifyCodeController {

    /**
     * 生成验证码
     */
    def index() {
        response.setHeader("Pragma", "No-cache")
        response.setHeader("Cache-Control", "no-cache")
        response.setDateHeader("Expires", 0)
        response.setContentType("image/jpeg")

        //生成随机字串  
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4)
        //存入cookie
        Cookie cookie = new Cookie("verifyCode",verifyCode.toLowerCase())
        cookie.setMaxAge(30 * 60) //set 30min
        cookie.setPath("/")
        cookie.setDomain("127.0.0.1")
        response.addCookie(cookie)

        //生成图片  
        int w = 150, h = 38
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode)
    }
}
